package com.tucc.hello.tree;

import lombok.Data;

import java.util.Optional;

/**
 * @author tucc
 * @description TODO
 * @date 2021/5/10 13:47
 **/

@Data
public class TreeNode {

    public int val;

    public TreeNode left;

    public TreeNode right;

    public TreeNode(int val) {
        this.val = val;
    }

    public static void printPreOrder(TreeNode node) {
        System.out.print(node.val + " ");
        Optional.ofNullable(node.left).ifPresent(o -> {
            printPreOrder(o);
        });
        Optional.ofNullable(node.right).ifPresent(o -> {
            printPreOrder(o);
        });
    }

    public static void printMiddleOrder(TreeNode node) {
        Optional.ofNullable(node.left).ifPresent(o -> {
            printMiddleOrder(o);
        });
        System.out.print(node.val + " ");
        Optional.ofNullable(node.right).ifPresent(o -> {
            printMiddleOrder(o);
        });
    }

    public static void printPostOrder(TreeNode node) {
        Optional.ofNullable(node.left).ifPresent(o -> {
            printPostOrder(o);
        });
        Optional.ofNullable(node.right).ifPresent(o -> {
            printPostOrder(o);
        });
        System.out.print(node.val + " ");
    }

}
